package com.kdtech.analyse.news;
import com.kdtech.analyse.AnalyseNews;
import com.kdtech.utils.HtmlCleaner;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.RegexUtils;

public class ChunyunNewsAnalyse implements AnalyseNews{

	private static final String[] regex={
//				"http://www.chunyun.cn/.*[0-9]{8}/[0-9]*.html",
//				"http://www.chunyun.cn/.*[0-9]*.html",
			"http://www.chunyun.cn/[a-z\\/]+[0-9\\/]+.html"
				};
		
		public boolean isDetailPage(String url) {
			return RegexUtils.matchAnyIgnoreCase(url, regex);
		}

		
		public NewsMeta Update(NewsMeta meta) {
			return null;
		}

		
		public NewsMeta parserHtml(UrlMeta urlMeta) {
			NewsMeta news=new NewsMeta();
			if (urlMeta.getHtml() == null) {
			}
			String htmltxt=urlMeta.getHtml();
			String url=urlMeta.getUrl();
			if(!isDetailPage(url)){
			}


			String title=null;
			String content=null;
			String author=null;
			Long date=null;
			Document doc=Jsoup.parse(htmltxt);

			doc.select(".hotline").remove();
			doc.select(".reader").remove();
			doc.select(".sidebar").remove();

			title=doc.select("div.title").text();
			if(StringUtils.isBlank(title)){
				title=doc.select(".main h2").text();
			}
			if(StringUtils.isBlank(title)){
				title=doc.select("h1").text();
			}
			content=HtmlCleaner.getContentHtml(url ,doc.select("div.maintext "));
			if(StringUtils.isBlank(content)){
				content=HtmlCleaner.getContentHtml(url ,doc.select("div.content "));
			}
			date=DateUtils.matchDate(doc.select("div.info").text());
			if(date == null){
				date=DateUtils.matchDate(doc.select("div.souce").text());
			}
			author=JSoupUtils.matchAuthor(doc, "来源:");
			news.setUrl(url);
			news.setTitle(title);
			news.setContent(content);
			news.setDate(date);
			news.setAuthor(author);
			return news;
		}

		public static void main(String[] args) {
			ChunyunNewsAnalyse test=new ChunyunNewsAnalyse();

			String url="http://www.chunyun.cn/tielujianshe/20161006/230799.html";
			UrlMeta meta=CrawlHTML.responseToURL(url);
			NewsMeta parserHtml=test.parserHtml(meta);
			System.out.println(parserHtml);
		}
	
	public boolean isNeedUpdate(){
		return false;
	}
}
